//
//  ViewController.swift
//  Runner
//
//  Created by 李旭峰 on 2020/10/10.
//

import UIKit
import SnapKit

class ViewController: UIViewController {
    override func loadView() {
        super.loadView()
        let lb = UILabel()
        lb.text = "this a native page"
        lb.textColor = UIColor.red
        lb.font = UIFont.systemFont(ofSize: 18)
        self.view.addSubview(lb)
        lb.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(120)
            make.centerX.equalToSuperview()
        }
        let btn_01 = UIButton(type: .custom)
        btn_01.setTitle("Push Flutter Page", for: .normal)
        btn_01.setTitleColor(UIColor.blue, for: .normal)
        btn_01.addTarget(self, action: #selector(onClickPushNativePage(_:forEvent:)), for: .touchUpInside)
        self.view.addSubview(btn_01)
        btn_01.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.size.equalTo(CGSize(width: 80, height: 44))
            make.top.equalTo(lb.snp.bottom).offset(20)
        }
        let btn_02 = UIButton(type: .custom)
        btn_02.setTitle("Present Flutter Page", for: .normal)
        btn_02.setTitleColor(UIColor.blue, for: .normal)
        btn_02.addTarget(self, action: #selector(onClickPresentFlutterPage(_:forEvent:)), for: .touchUpInside)
        self.view.addSubview(btn_02)
        btn_02.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.size.equalTo(CGSize(width: 80, height: 44))
            make.top.equalTo(btn_01.snp.bottom).offset(20)
        }
        let btn_03 = UIButton(type: .custom)
        btn_03.setTitle("Push Native Page", for: .normal)
        btn_03.setTitleColor(UIColor.blue, for: .normal)
        btn_03.addTarget(self, action: #selector(onClickPushNativePage(_:forEvent:)), for: .touchUpInside)
        self.view.addSubview(btn_03)
        btn_03.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.size.equalTo(CGSize(width: 80, height: 44))
            make.top.equalTo(btn_02.snp.bottom).offset(20)
        }
        
        let btnSend_01 = UIButton(type: .custom)
        btnSend_01.setTitle("send 01", for: .normal)
        btnSend_01.setTitleColor(UIColor.blue, for: .normal)
        btnSend_01.addTarget(self, action: #selector(onClickSend01(_:)), for: .touchUpInside)
        self.view.addSubview(btnSend_01)
        btnSend_01.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview().multipliedBy(0.5)
            make.size.equalTo(CGSize(width: 80, height: 44))
            make.bottom.equalToSuperview().offset(-120)
        }
        let btnSend_02 = UIButton(type: .custom)
        btnSend_02.setTitle("send 02", for: .normal)
        btnSend_02.setTitleColor(UIColor.blue, for: .normal)
        btnSend_02.addTarget(self, action: #selector(onClickSend01(_:)), for: .touchUpInside)
        self.view.addSubview(btnSend_02)
        btnSend_02.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview().multipliedBy(1.5)
            make.size.equalTo(CGSize(width: 80, height: 44))
            make.bottom.equalToSuperview().offset(-120)
        }
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
//        self.setName("flutter://EmptyFlutterPage", params: [:])
        TestEventManager.default.initEventChannel(with: self)
    }

    @IBAction func onClickPushFlutterPage(_ sender: UIButton, forEvent event: UIEvent){
//        FlutterBoostPlugin.open(kFlutterFirstViewPageUrl, urlParams:[kPageCallBackId:"MycallbackId#1"], exts: ["animated":true], onPageFinished: { (_ result:Any?) in
//            print(String(format:"call me when page finished, and your result is:%@", result as! CVarArg));
//        }) { (f:Bool) in
//            print(String(format:"page is opened"));
//        }
    }
    @IBAction func onClickPresentFlutterPage(_ sender: UIButton, forEvent event: UIEvent){
//        FlutterBoostPlugin.present(kFlutterSecondViewPageUrl, urlParams:[kPageCallBackId:"MycallbackId#2"], exts: ["animated":true], onPageFinished: { (_ result:Any?) in
//            print(String(format:"call me when page finished, and your result is:%@", result as! CVarArg));
//        }) { (f:Bool) in
//            print(String(format:"page is presented"));
//        }
    }
    
    @IBAction func onClickPushNativePage(_ sender: UIButton, forEvent event: UIEvent) {
//        FlutterBoostPlugin.open(kNativeFirstViewPageUrl, urlParams: [:], exts: [:], onPageFinished: { (_ result:Any?) in
//            print(String(format:"call me when page finished, and your result is:%@", result as! CVarArg));
//        }) { (f:Bool) in
//            print(String(format:"page is presented"));
//        }
    }

    @IBAction func onClickSend01(_ sender: UIButton) {
//        FlutterBoostPlugin.sharedInstance().sendEvent("App/Event/Channel", arguments: ["1234":"qwer"])
//        let messenger = FlutterBoostPlugin.sharedInstance().currentViewController() as! FlutterBinaryMessenger
//        let text = "send test"
//        let textData = text.data(using: .utf8)
//        messenger.send(onChannel: "App/Event/Channel", message: textData)
    }
    @IBAction func onClickSend02(_ sender: UIButton) {
        
    }
}
